package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.BooksDao;
import com.example.domain.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BooksDao booksDao;

    /*
    分页查询书籍
     */
    public Page<Books> selectBookByPage(String name,Integer current,Integer size){
        QueryWrapper<Books> qw = new QueryWrapper<>();
//        按照搜索查询，name要是不为空就进行模糊查询
        qw.like(StringUtils.isNotBlank(name),"bookName",name);
//        分页查询，前提需要用到分页查询的插件，也就是config包下的类
        Page<Books> bookPage = new Page<>();
//        设置当前页
        bookPage.setCurrent(current);
//        设置每页展示的数据
        bookPage.setSize(size);
//        调用mybatis-plus里面封装好的方法
        return booksDao.selectPage(bookPage, qw);
    }

    /*
    根据id查询书籍
     */
    public Books selectById(Long id){
        Books books = booksDao.selectById(id);
        return books;
    }

    /*
    添加书籍
     */
    public void addBook(Books book){
        booksDao.insert(book);
    }

    /*
    修改书籍
     */
    public void updateBook(Books book){
        QueryWrapper<Books> qw = new QueryWrapper<>();
        qw.eq("id",book.getId());
        booksDao.update(book,qw);
    }

    /*
    删除书籍
     */
    public void delBookById(Long id){
        booksDao.deleteById(id);
    }

    /*
    批量删除
     */
    public void delBookByIds(List<Long> ids){
        booksDao.deleteBatchIds(ids);
    }

    public List<Books> selectAll(){
        return booksDao.selectList(null);
    }
}
